﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统部门仓储
/// </summary>
public class SysDeptRep : BaseRep<SysDeptModel>
{
    private static string sqlFields = string.Format(@"
         `dept_id`
        ,`parent_id`
        ,`ancestors`
        ,`dept_name`
        ,`order_num`
        ,`leader`
        ,`phone`
        ,`email`
        ,`status`
        ,`del_flag`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
    ");

    /// <summary>
    /// 通过部门编号获取系统部门信息
    /// </summary>
    /// <param name="deptId">部门编号</param>
    /// <returns>SysDeptModel?</returns>
    public SysDeptModel? GetSysDeptByDeptId(long deptId)
    {
        string sqlWhere = " and `dept_id` = @DeptId ";

        return QuerySingle(sqlFields, sqlWhere, new { DeptId = deptId });
    }

    /// <summary>
    /// 获取系统部门列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysDeptModel></returns>
    public IEnumerable<SysDeptModel> GetSysDeptList(SysDeptModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ParentId > -1)
        {
            sqlWhere += " and parent_id = @ParentId ";
        }
        if (!string.IsNullOrWhiteSpace(model.Ancestors))
        {
            sqlWhere += " and find_in_set(@Ancestors, ancestors) ";
        }
        if (!string.IsNullOrWhiteSpace(model.DeptName))
        {
            sqlWhere += " and dept_name like concat('%', @DeptName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Leader))
        {
            sqlWhere += " and leader like concat('%', @Leader ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and phone like concat('%', @Phone ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and email like concat('%', @Email ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取系统部门列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysDeptModel></returns>
    public Page<SysDeptModel> GetSysDeptPageList(SysDeptModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ParentId > -1)
        {
            sqlWhere += " and parent_id = @ParentId ";
        }
        if (!string.IsNullOrWhiteSpace(model.Ancestors))
        {
            sqlWhere += " and find_in_set(@Ancestors, ancestors) ";
        }
        if (!string.IsNullOrWhiteSpace(model.DeptName))
        {
            sqlWhere += " and dept_name like concat('%', @DeptName ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Leader))
        {
            sqlWhere += " and leader like concat('%', @Leader ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and phone like concat('%', @Phone ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and email like concat('%', @Email ,'%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过部门编号更新系统部门信息
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysDeptByDeptId(SysDeptModel model)
    {
        string sqlSet = string.Empty;

        #region ===set===
        if (model.ParentId > -1)
        {
            sqlSet += " parent_id = @ParentId, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Ancestors))
        {
            sqlSet += " ancestors = @Ancestors, ";
        }

        if (!string.IsNullOrWhiteSpace(model.DeptName))
        {
            sqlSet += " dept_name = @DeptName, ";
        }

        if (model.OrderNum > -1)
        {
            sqlSet += " order_num = @OrderNum, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Leader))
        {
            sqlSet += " leader = @Leader, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlSet += " phone = @Phone, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlSet += " email = @Email, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " status = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " update_by = @UpdateBy, ";
        }

        sqlSet += " update_time = sysdate() ";
        #endregion

        string sqlWhere = " and dept_id = @DeptId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过系统部门编号删除系统部门
    /// </summary>
    /// <param name="deptId">系统部门编号</param>
    /// <returns>int</returns>
    public int DeleteSysDictDataByDictCode(long deptId)
    {
        string sqlWhere = " and `dept_id` = @DeptId ";

        return Delete(sqlWhere, new { DeptId = deptId });
    }
}
